package in.muton.edushare.pages.user;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.helper.TinyMCEHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.panels.ImageHolder;
import in.muton.edushare.panels.upload.single.SingleUploadButtonPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import wicket.contrib.tinymce.TinyMceBehavior;
import wicket.contrib.tinymce.settings.TinyMCESettings;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.file.image.ProfileImages;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.Users;

public class EditProfileGeneralPanel extends EdusharePanel {

	private static final long serialVersionUID = 9072069723483655339L;
	private IModel<Users> userModel;

	public EditProfileGeneralPanel(String id, IModel<Users> model, EduShareBasePage page) {
		super(id, model, page);
		this.userModel = model;
		loadPanel();
	}

	private void loadPanel() {
		Form<Void> genForm = new Form<Void>("genForm"){
			private static final long serialVersionUID = -1251063593087997416L;

			@Override
			protected void onSubmit() {
				if(userModel.getObject().getFirstName()!=null && userModel.getObject().getFirstName().length() > ApplicationConstants.MAX_TEXT_FIELD_SIZE){
					setError("Error: First name is too long.");
					return;
				}else if(userModel.getObject().getLastName()!=null && userModel.getObject().getLastName().length()>ApplicationConstants.MAX_TEXT_FIELD_SIZE){
					setError("Error: Last Name is too long.");
					return;
				}else if(userModel.getObject().getPhoneNo()!=null && userModel.getObject().getPhoneNo().length() > 90){
					setError("Error: Phone number is too long");
					return;
				}else if(userModel.getObject().getEmail()!=null && userModel.getObject().getEmail().length() > ApplicationConstants.MAX_TEXT_FIELD_SIZE){
					setError("Error: Email address is too long.");
					return;
				}else if(userModel.getObject().getInterests()!=null && userModel.getObject().getInterests().length() > ApplicationConstants.MAX_DESC_SIZE){
					setError("Error: Interests field is too long.");
					return;
				}else if(userModel.getObject().getHonors()!=null && userModel.getObject().getHonors().length() > ApplicationConstants.MAX_DESC_SIZE){
					setError("Error: Honors and Awards fiels is too long.");
					return;
				}else if(userModel.getObject().getExpertise()!=null && userModel.getObject().getExpertise().length() > ApplicationConstants.MAX_DESC_SIZE){
					setError("Error: Expertise field is too long.");
					return;
				}else if(userModel.getObject().getAboutMe()!=null && userModel.getObject().getAboutMe().length() > ApplicationConstants.MAX_DESC_SIZE){
					setError("Error: Summary is too long.");
					return;
				}
				getORMInterface().commitAllChanges();
				clearError();
			}
		};
		
		add(genForm);
		genForm.add(new TextField<String>("fName", new PropertyModel<String>(userModel, "firstName")));
		genForm.add(new TextField<String>("lName", new PropertyModel<String>(userModel, "lastName")));
		genForm.add(new TextField<String>("phNo", new PropertyModel<String>(userModel, "phoneNo")));
		genForm.add(new TextField<String>("email", new PropertyModel<String>(userModel, "email")));
		TextArea<String> abtMe =  new TextArea<String>("abtMe", new PropertyModel<String>(userModel, "aboutMe"));
		abtMe.add(TinyMCEHelper.getDefaultTinyMCEBehavior());
		genForm.add(abtMe);
		genForm.add(new TextField<String>("skills", new PropertyModel<String>(userModel, "expertise")));
		TextArea<String> honors = new TextArea<String>("honors", new PropertyModel<String>(userModel, "honors"));
		honors.add(TinyMCEHelper.getDefaultTinyMCEBehavior());
		genForm.add(honors);
		TextArea<String> interests = new TextArea<String>("hobbies", new PropertyModel<String>(userModel, "interests"));
		interests.add(TinyMCEHelper.getDefaultTinyMCEBehavior());
		genForm.add(interests);
		
		// image
		Form<Void> uploadForm = new Form<Void>("uploadForm");
		genForm.add(uploadForm);
		final ImageHolder imageHolder;
		genForm.add(imageHolder = new ImageHolder("profileImage", new PropertyModel<String>(EditProfileGeneralPanel.this, "imgUrl")));
		imageHolder.setOutputMarkupId(true);
		final ImageHolder imageHolderPreview;
		genForm.add(imageHolderPreview = new ImageHolder("profileImagePreview", new PropertyModel<String>(EditProfileGeneralPanel.this, "imgUrl")));
		imageHolderPreview.setOutputMarkupId(true);
		
		FileDirectories gallery = new FileDirectoriesDAO(EduShareSession.get().getSessionORMInterface())
																		.getOrCreateDefaultGallery(this.userModel.getObject());
		gallery.setFileProcessStrategy(new ProfileImages());
		
		uploadForm.add(new SingleUploadButtonPanel("uploadImg", new CayenneModel<FileDirectories>(gallery)) {
			
			private static final long serialVersionUID = -8922684853450131201L;

			@Override
			protected void onUploadWindowClose(AjaxRequestTarget target) {
				if(EduShareSession.get().getFileUploadedInLastUpload()!=null && 
						EduShareSession.get().getFileUploadedInLastUpload().size()>0){
					
					Users u = getLoggedInUser();
					u.setImageFile(EduShareSession.get().getFileUploadedInLastUpload().get(0));
//					AccountSettingsTab.this.setVbadgeUrl(u);
					EduShareSession.get().getSessionORMInterface().commitAllChanges();
					target.addComponent(imageHolder);
					target.addComponent(imageHolderPreview);
				}
					
			}
		});
		
	}
	
	public String getImgUrl(){
		String url = this.userModel.getObject().getUploadedFileUrl();
		return URLUtils.getFileVersionUrlFromCompleteUrl(url, ApplicationConstants.FILE_SUFFIX_HBADGE);
	}
}
 